

from operator import index
from re import S
from textwrap import indent


class Node(object):
    def __init__(self,item) -> None:
        self.item = item
        self.next = None
        self.prior = None
    def  __repr__(self) -> str:
        return str(self.item)

class Linklist(object):
    def __init__(self) -> None:
        self.head = None
        self.tail = None
        self.num = 0
    
    def Tcreate_linklist(self,li):
        self.head = Node(li[0])
        self.num = 0
        curNode = self.head
        for element in li[1:]:
            newNode = Node(element)
            newNode.prior = curNode
            curNode.next = newNode
            curNode = newNode
            self.num += 1
        self.tail = curNode
    
    def Hcreate_linklist(self,li):
        self.tail = Node(li[0])
        self.head = self.tail
        curNode = self.head
        self.num = 0
        for element in li[1:]:
            newNode = Node(element)
            newNode.next = curNode
            curNode.prior = newNode
            curNode = newNode
            self.num += 1
        self.head = curNode

    def search_index(self,index:int): #按索引搜索
        if index >= 0 and index < self.num:
            curNode = self.head
            for _ in range(index):
                curNode = curNode.next
            return curNode
        elif index < 0 and index >= -self.num:
            curNode = self.tail
            for _ in range(index, 0,1):
                curNode = curNode.prior
            return curNode
        else:
            raise IndexError("The Index is out of range")

    def search_element(self,item:any): #按值搜索引
        num = 0
        li = self.head
        while li.item != item:
            li = li.next
            num += 1
        return li

    def insert_item(self,item:any,index:int):
        node = Node(item)
        self.__insertNode(node,index)


    def __insertNode(self,node:Node,index:int): #插入
        if index > 0 and index < 0:
            curNode = self.head
            for _ in range(0,index):
                curNode = curNode.next
            curNode.prior.next = node
            node.prior = curNode.prior
            curNode.prior = node
            node.next = curNode 
        elif index == 0:
            node.next = self.head
            self.head.prior = node
            self.head = node
        else:
            raise IndexError("The Index is out of range")

    def deleteItem(self,index:int):
        if index > 0 and index < self.num:
            curNode = self.head
            num = 0
            for _ in range(0,index):
                curNode = curNode.next
                num += 1
            curNode.prior.next = curNode.next
            curNode.next.prior = curNode.prior
        elif index == 0:
            curNode = self.head
            

    def print_linklist(self):
        ele = self.head
        while ele:
            print(ele,end=" ")
            ele = ele.next
        print()


if __name__ == "__main__":
    alink = Linklist()
    alink.Hcreate_linklist([1,2,3,5,7,6,4])
    alink.print_linklist()
    print(alink.search_index(0))
    print(alink.search_index(0))
    print(alink.search_element(5))